#include "bhxjasynworker.h"

BHXJAsynWorker::BHXJAsynWorker()
{
    this->workthread=new thread([this]()
    {
        while (true)
        {
            unique_lock<mutex> lock{_lock};
            _task_cv.wait(lock,[this]()
                          {
                              return !(_run&&_tasks.empty());
                          });
            if ((!this->_run) && this->_tasks.empty())
                return;
            Task task=move(_tasks.front());
            _tasks.pop();
            lock.unlock();

            std::stringstream stream;
               stream << workthread->get_id();
            std::cout<<workthread->get_id()<<endl;
            task();

        }
    });
}
BHXJAsynWorker::~BHXJAsynWorker()
{
    this->_run=false;
    this->_task_cv.notify_all();
    this->workthread->join();
    delete this->workthread;
}
